/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package pl.droidsonroids.gif.gifsample.opengl;

import static ohos.agp.render.opengl.EGL.EGL_NO_CONTEXT;
import static ohos.agp.render.opengl.EGL.EGL_NO_DISPLAY;
import static ohos.agp.render.opengl.EGL.EGL_NO_SURFACE;
import static ohos.agp.render.opengl.EGL.eglDestroyContext;
import static ohos.agp.render.opengl.EGL.eglDestroySurface;
import static ohos.agp.render.opengl.EGL.eglMakeCurrent;
import static ohos.agp.render.opengl.EGL.eglReleaseThread;
import static ohos.agp.render.opengl.EGL.eglTerminate;

import ohos.agp.render.opengl.EGL;
import ohos.agp.render.opengl.EGLConfig;
import ohos.agp.render.opengl.EGLContext;
import ohos.agp.render.opengl.EGLDisplay;
import ohos.agp.render.opengl.EGLSurface;

/**
 * EglConnection
 */
public class EglConnection {
    private static final int EIGHT = 8;
    private static final int TWO = 2;
    private static final int THREE = 3;
    private static final int HEXA_64 = 0x0040;
    private static final int HEXA_12440 = 0x3098;

    private EGLDisplay eglDisplay = EGL_NO_DISPLAY;
    private EGLSurface eglSurface = EGL_NO_SURFACE;
    private EGLContext eglContext = EGL_NO_CONTEXT;

    /**
     * Initialize the EGL
     *
     * @param window surface
     * */
    public final void init(Object window) {
        eglDisplay = EGL.eglGetDisplay(EGL.EGL_DEFAULT_DISPLAY);
        if (eglDisplay == EGL.EGL_NO_DISPLAY) {
            return;
        }
        int[] majorVersion = new int[TWO];
        int[] minorVersion = new int[TWO];
        if (!EGL.eglInitialize(eglDisplay, majorVersion, minorVersion)) {
            return;
        }
        int[] attributes = new int[] {
            EGL.EGL_RED_SIZE, EIGHT,
            EGL.EGL_GREEN_SIZE, EIGHT,
            EGL.EGL_BLUE_SIZE, EIGHT,
            EGL.EGL_ALPHA_SIZE, EIGHT,
            EGL.EGL_DEPTH_SIZE, EIGHT,
            EGL.EGL_RENDERABLE_TYPE, HEXA_64,
            EGL.EGL_NONE};
        int[] numConfig = new int[1];
        if (!EGL.eglChooseConfig(eglDisplay, attributes, null, 1, numConfig)) {
            return;
        }
        int numConfigs = numConfig[0];
        if (numConfigs <= 0) {
            return;
        }
        EGLConfig[] configs = new EGLConfig[numConfigs];
        if (!EGL.eglChooseConfig(eglDisplay, attributes, configs, numConfigs, numConfig)) {
            return;
        }
        int[] contextAttr = new int[] {
            HEXA_12440, THREE,
            EGL.EGL_NONE
        };
        eglContext = EGL.eglCreateContext(eglDisplay, configs[0], EGL.EGL_NO_CONTEXT, contextAttr);

        eglSurface = EGL.eglCreateWindowSurface(eglDisplay, configs[0], window, null);
        if (eglSurface == EGL.EGL_NO_SURFACE) {
            return;
        }
        if (!EGL.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) {
            return;
        }
    }

    /**
     * Draw
     *
     * */
    public final void draw() {
        EGL.eglSwapBuffers(this.eglDisplay, this.eglSurface);
    }

    /**
     * Destroy the EGL connection
     *
     * */
    public final void destroy() {
        if (eglDisplay != EGL_NO_DISPLAY) {
            eglMakeCurrent(eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
            eglDestroySurface(eglDisplay, eglSurface);
            eglSurface = EGL_NO_SURFACE;
            eglDestroyContext(eglDisplay, eglContext);
            eglContext = EGL_NO_CONTEXT;
            eglTerminate(eglDisplay);
            eglDisplay = EGL_NO_DISPLAY;
            eglReleaseThread();
        }
    }
}
